package com.penghk.hangzhouhouseprice.listener;

import org.springframework.stereotype.Component;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取所有的session
 */
@Component
@WebListener
public class HttpSessionListenerImpl implements HttpSessionListener {
    public static Map<String , HttpSession> sessionMap = new ConcurrentHashMap<>();

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        sessionMap.put(se.getSession().getId() , se.getSession());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("destroy session :"+se.getSession().getId());
        sessionMap.remove(se.getSession().getId());
    }

}
